home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6880 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  909 b 

  1. Path: hecate.umd.edu!bs05
  2. From: bs05@csc.umd.edu (CMSC 420)
  3. Newsgroups: comp.lang.c++
  4. Subject: help with stream read/write
  5. Date: 20 Feb 1996 20:47:14 GMT
  6. Organization: University of Maryland, College Park
  7. Message-ID: <4gdc0i$cje@hecate.umd.edu>
  8. NNTP-Posting-Host: holmes.umd.edu
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I'm trying to write an array of floats to a file and then read it back.
  12. When I do, everything is ok for the first 141 array elements (a[0] to a[140]).
  13. After that, the array is garbage.  Can someone look at the code snippet?
  14.  
  15.     const int len = 200;
  16.     float a[len];
  17.  
  18.     // fill array
  19.     float x = 0.0;
  20.     for (int n=0; n<len; ++n) 
  21.         a[n] = (x += 1.0);
  22.  
  23.     // write array to file
  24.     ofstream outfile("data");
  25.     outfile.write((unsigned char*) a, len*sizeof(float));    
  26.     outfile.close();
  27.  
  28.     // read array from file
  29.     ifstream infile("data");
  30.     infile.read((unsigned char*) a, len*sizeof(float));    
  31.     infile.close();
  32.  
  33.